<?php
	session_start();
	//操作订单和订单详情
	include("dbconfig.php");
	$link = @mysqli_connect(HOST,USER,PASS) or die("失败");
	mysqli_select_db($link,DBNAME);
	mysqli_set_charset($link,"utf8");
	
	//var_dump($_GET);
	//var_dump($_POST);die;
	switch($_GET['a']){
		case "add":
		//1 接收表单传递的信息
		$uid = $_SESSION['user']['id'];
		$linkman = $_POST['linkman'];
		$address = $_POST['address'];
		$code = $_POST['code'];
		$phone = $_POST['phone'];
		$addtime = time();
		$total = $_SESSION['total'];
		//var_dump($_POST);die;
		//2 写sql语句
		$sql = "insert into orders values(null,$uid,'$linkman','$address',$code,$phone,$addtime,$total,1)";
		//echo $sql;die;
		mysqli_query($link,$sql);
			//插入到订单表
		if(mysqli_insert_id($link)>0){
			$orderid = mysqli_insert_id($link);//产生的订单id号
			//插入到订单详情表
			// echo $orderid;die;
			// var_dump($_SESSION['goods']);die;
			foreach($_SESSION['goods'] as $k=>$v){
				$sql = "insert into detail(id,orderid,goodsid,name,price,num) values(null,$orderid,$k,'{$v['goods']}','{$v['price']}',{$v['num']})";//注意此处标点符号的问题				
				// echo $sql;die;
				mysqli_query($link,$sql);
				// echo mysqli_insert_id($link);die;
				
			}			
			unset($_SESSION['goods']);//清空购物车
			unset($_SESSION['total']);//清空总金额
			header('location:DONE2.php');
			echo "新生成的订单号为: ".$orderid;
		}else{
			echo "订单生成失败";
		}
		break;
	}
	